'''
此文件是监控爬虫的运行
如果其中一个爬虫断开，自动检测，开启爬虫
这是在 linux中运行的命令

ps -ef | grep zhihu

root      9329 25213  0 17:12 ?        00:00:00 /home/wangdu/anaconda3/bin/python3.7 /home/wangdu/git_pro/32_spider/zhihu_spider/zhihuSpider/zhihuSpiderFollow.py
root      9443 25213  0 14:09 ?        00:01:01 /home/wangdu/anaconda3/bin/python3.7 /home/wangdu/git_pro/32_spider/zhihu_spider/zhihuSpider/zhihuSpiderUser.py

选中其中一个
    ps -ef | grep zhihu | grep Follow
    ps -ef | grep zhihu | grep User


这个模块可以运行shell命令，将结果返回
import subprocess as subp
key, result = subp.getstatusoutput('ps -ef | grep zhihu | grep Follow')
key  命令是否运作成功  0成功  1错误
result  命令返回的结果

'''

import subprocess as subp
from threading import Thread
import time


def followFun():
    while True:
        follow_spider = subp.getstatusoutput('ps -ef | grep zhihuSpiderFollow.py | grep -v grep')
        user_spider = subp.getstatusoutput('ps -ef | grep zhihuSpiderUser.py | grep -v grep')

        if 0 == follow_spider[0]:
            if follow_spider[1].find('zhihuSpiderFollow.py') == -1:
                def aaa():
                    follow_run = subp.getstatusoutput(
                        '/home/wangdu/anaconda3/bin/python3.7 /home/wangdu/git_pro/32_spider/zhihu_spider/zhihuSpider/zhihuSpiderFollow.py')

                th = Thread(target=aaa)
                th.setDaemon(True)
                th.start()
            else:
                print('zhihuSpiderFollow runing')

        if 0 == user_spider[0]:
            if user_spider[1].find('zhihuSpiderUser.py') == -1:

                def bbb():
                    subp.getstatusoutput(
                        '/home/wangdu/anaconda3/bin/python3.7 /home/wangdu/git_pro/32_spider/zhihu_spider/zhihuSpider/zhihuSpiderUser.py')

                th = Thread(target=bbb)
                th.setDaemon(True)
                th.start()
            else:
                print('zhihuSpiderUser runing')

        time.sleep(16)

followFun()